package thread;

class Counter2{
    private int count = 0;

    public int getCount() {
        return count;
    }

    synchronized public void add() {
        count++;
    }
}
public class Demo21 {
    public static void main(String[] args) throws InterruptedException {
        Counter2 counter2 = new Counter2();

        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter2.add();
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter2.add();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count = " + counter2.getCount());
    }
}
